package com.peng.leetcode.tree;

import java.util.*;

/**
 * IsCompleteTree1
 * 958. 二叉树的完全性检验
 * https://leetcode.cn/problems/check-completeness-of-a-binary-tree/
 *
 * @author: lupeng6
 * @create: 2023/2/12 15:17
 */
public class IsCompleteTree1 {

    public static void main(String[] args) {
        TreeNode root = TreeMain.array2tree(new Integer[]{1, 2, 3, 4, 5, null, 7});
        new IsCompleteTree1().isCompleteTree(root);
    }

    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        boolean reachedEnd = false;
        while (!q.isEmpty()) {
            TreeNode cur = q.poll();
            if (reachedEnd && cur != null) {
                return false;
            }
            if (cur == null) {
                reachedEnd = true;
                continue;
            }
            q.offer(cur.left);
            q.offer(cur.right);
        }
        return true;
    }
}
